package com.globant.rdm.domain.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.SnowballPorterFilterFactory;
import org.apache.solr.analysis.StandardTokenizerFactory;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;

import com.globant.rdm.domain.service.TimeService;
import com.google.inject.Inject;

@Entity
@Indexed
@AnalyzerDef(name = "customanalyzer",
		  tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
		  filters = {
		    	@TokenFilterDef(factory = LowerCaseFilterFactory.class),
		    	@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
		    	@Parameter(name = "language", value = "Spanish")
		    })
		  })
public class Employee {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Field(index=Index.TOKENIZED, store=Store.YES)
	private String name;

	@Field(index=Index.TOKENIZED, store=Store.NO)
	private String timestamp;

	@Analyzer(definition = "customanalyzer")
	@Field(index=Index.TOKENIZED, store=Store.YES)
	private String description;
	
	@Transient
	@Inject
	private TimeService timeService;
	
	public Employee() {
	}

	public Employee(final Long id, final String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Employee - " + id + " - " + name + " - " + timestamp + " - " + description;
	}
	
	public void updateTimestamp() {
		timestamp = timeService.getTime().toString();
	}
	
	public Employee changeName(final String newName) {
		name = newName;
		return this;
	}

	public Employee setDescription(String description) {
		this.description = description;
		return this;
	}
	
}
